<?php

session_start();

if(isset($_SESSION['loggedin'])){
	include 'adminfunc.php';
	include '../func/sql.php';
	include '../func/addPar.php';
	include '../func/get.php';
	include '../func/check.php';
	include 'delete.php';
	
	if(isset($_POST['event']) && isset($_POST['total'])){
		
		$event = $_POST['event'];
		$total = $_POST['total'];
		
		if(isset($_POST['del'])){
			$del = $_POST['del'];
		}else{
			$del = array();
		} 
		
		if(isset($_POST['edit'])){	
			$edit = $_POST['edit'];
		}else{
			$edit = array(); 
		}
		
		if(!checkArrays($del,$edit)){
			$outMsg = "";
			$count = $_POST['total'];
				
			if(count($edit) > 0){
				$i = 0;
				$sql = "SELECT SN,paidStatus FROM event_" . $event;
				$res = sendQ($sql);
				
				while(($row = mysql_fetch_array($res)) != null){
					if(array_key_exists($i,$edit)){
						//WANNA
						//echo "Paid->" . $row['SN'] . "<br>";
						editPar($event,$row['SN'],1);
					}else{
						//NO PAY..
						//echo "NO Paid->" . $row['SN'] . "<br>";
						editPar($event,$row['SN'],0);
					}						
					$i++;
				}	
			}
			
			$c = 0;
			if(count($del) > 0){
				foreach($del as $d){
					if(!deletePar($event,$d)){
						$outMsg .= '<p>Could not delete SN: ' . $d . '</p>';	
					}else{
						if(!isWLEmpty($event)){
								$ns = newSpot($event);
								if($ns){
									//Moved..
									$c++;
								}else if($ns == null){
									//No one to move..
								}else{
									$outMsg .= '<p>Could not make new spot...</p>';
								}
						}

					}
				}
			}
			
		}
		
		if($c > 0){
			$outMsg .= "<p>Moved " . $c . " people from the waitlist</p>";
			if($c < count($del)){
				$outMsg .= "<p>No people on the wait list</p>";
			}
		}
		
		$_SESSION['msg'] = $outMsg;
		header("Location: index.php?event=" . $event . "&edit");
	}
	
}else{
	header("Location: index.php");
}
?>
